<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/8/24
 * Time: 15:46
 */
namespace app\data;

use app\common\Common;
use think\Db;

class WithdrawData extends Base
{
    /**
     * 添加提现
     * @param $user_id
     * @param $order_num
     * @param $total_money
     * @param $time
     * @param $money
     * @param $fees
     * @param $fees_proportion
     * @param $review_type
     * @return bool
     */
    public function addWithdraw($user_id, $order_num, $total_money, $time, $money, $fees, $fees_proportion,$review_type)
    {
        Db::table('drh_withdraw')
            ->insert([
                'user_id' => $user_id,
                'order_num' => $order_num,
                'total_money' => $total_money,
                'money' => $money,
                'fees' => $fees,
                'create_time' => $time,
                'fees_proportion' => $fees_proportion,
                'review_type'=>$review_type
            ]);
        return true;
    }

    public function withdrawShow($page, $pre, $sort, $where)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_withdraw a')
            ->field([
                "FROM_UNIXTIME(a.create_time,'%Y-%m-%d') time",
                "a.review_type",//1商户  2 用户
                'a.total_money',
                'a.msg',
                'a.review_status',
                'a.order_num',
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function withdrawTotal($where)
    {
        $res = Db::table('drh_withdraw a')
            ->field([
                'count(*) count'
            ])->where($where)
            ->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function showWithdrawSuccessMoney($status,$id,$review_status=1)
    {
        $res=Db::table('drh_withdraw a')
            ->join('drh_user_info b','a.user_id=b.id','left')
            ->join('drh_shop c','a.user_id=c.user_id','left')
            ->field([
                'IFNULL(sum(a.total_money),0) money',
                'IFNULL(c.money,0) shop_money',
                'IFNULL(b.user_money,0)  user_money',
                'b.id user_id',
                'c.id shop_id',
            ])->where([
                'a.user_id'=>$id,
                'a.review_status'=>$review_status,
                'a.review_type'=>$status,
            ])->group('a.user_id')
            ->find();
       return $res;
    }
}